package com.mr.sell.controller;

import com.mr.sell.config.WechatAccountConfig;
import com.mr.sell.exception.CustomException;
import com.mr.sell.utils.ConstantEnum;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Class: WechatController
 * User (作者):MRui
 * TODO (描述)：微信接口
 * Date 2018.09.24 22:15
 */
@RestController
@RequestMapping("/wechat")
@Slf4j
public class WechatController {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WxMpService wxOpenService;

    /**
    * User (作者): MRui
    * TODO (描述)：用户网页授权
    * Data：2018.09.24 23:29
    */
    @PostMapping("authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        String url = wechatAccountConfig.getWechatMpAuthorize()+"外网服务器地址";
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, "");
        return "redirect:"+redirectUrl;
    }
    /**
    * User (作者): MRui
    * TODO (描述)： 获取用户openid
    * Data：2018.09.24 23:31   
    */
    @PostMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                         @RequestParam("state") String returnUrl){
        try {
            WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
            String openId = wxMpOAuth2AccessToken.getOpenId();
            return "redirect:"+returnUrl+"?openid="+openId;
        } catch (WxErrorException e) {
            log.error("[微信网页授权]{}",e);
            throw new CustomException(ConstantEnum.EXCEPTION_WECHAT_ERROR.getCode(),e.getError().getErrorMsg());
        }
    }

    /**
    * User (作者): MRui
    * TODO (描述)：二维码扫码登陆
    * Data：2018.10.03 22:32
    */
    @PostMapping("/qrAuthorize")
    public String qrAuthorize(@RequestParam("returnUrl") String returnUrl){
        String url = wechatAccountConfig.getWechatOpenAuthorize()+"外网服务器地址";
        String redirectUrl = wxOpenService.buildQrConnectUrl(url,WxConsts.QrConnectScope.SNSAPI_LOGIN,"");
        return "redirect:"+redirectUrl;
    }

    /**
    * User (作者): MRui
    * TODO (描述)：获取用户openid
    * Data：2018.10.03 22:35
    */
    @PostMapping("/qrUserInfo")
    public String qrUserInfo(@RequestParam("code") String code,
                           @RequestParam("state") String returnUrl){
        try {
            WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxOpenService.oauth2getAccessToken(code);
            String openId = wxMpOAuth2AccessToken.getOpenId();
            return "redirect:"+returnUrl+"?openid="+openId;
        } catch (WxErrorException e) {
            log.error("[微信网页授权]{}",e);
            throw new CustomException(ConstantEnum.EXCEPTION_WECHAT_ERROR.getCode(),e.getError().getErrorMsg());
        }
    }
}
